﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using StarsAdmin.Application.User.Dtos;
using StarsAdmin.Application.User.Services;
using StarsAdmin.Core.Attributes;
using StarsAdmin.Core.Consts;
using StarsAdmin.Core.Models;
using StarsAdmin.Web.Core.Conttrollers;
using System.ComponentModel.DataAnnotations;

namespace StarsAdmin.WebApi.Conttrollers
{
    /// <summary>
    /// 用户管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Order(2)]
    [Authorize(Policy = RoleTypeConst.UserAdmin)]
    public class UserController : BaseController
    {
        private readonly IUserService _userService;

        public UserController(IUserService userService)
        {
            _userService = userService;
        }

        /// <summary>
        /// 获取所有
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel<List<UserDto>>> GetAllAsync([FromQuery] BaseQueryInput input)
        {
            return (await _userService.GetAllAsync(input)).ResultFormat();
        }

        /// <summary>
        /// 分页获取
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel<Page<UserDto>>> GetPageAsync([FromQuery] PageQueryInput input)
        {
            return (await _userService.GetPageAsync(input)).ResultFormat();
        }

        /// <summary>
        /// 根据Id获取数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel<UserDto>> GetByIdAsync([Required] long id)
        {
            return (await _userService.GetByIdAsync(id)).ResultFormat("没有找到该条数据", 404);
        }

        /// <summary>
        /// 根据Id集合获取数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel<List<UserDto>>> GetByIdsAsync([FromQuery] BaseIdsInput input)
        {
            return (await _userService.GetByIdsAsync(input.Ids)).ResultFormat();
        }

        /// <summary>
        /// 批量导出
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> ExportAsync(BaseIdsInput input)
        {
            var result = await _userService.ExportAsync(input.Ids);
            return result != null ? result : NotFound();
        }

        /// <summary>
        /// 导出模版
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> ExportTemplateAsync()
        {
            var result = await _userService.ExportTemplateAsync();
            return result != null ? result : NotFound();
        }

        /// <summary>
        /// 批量导入
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<bool>> ImportAsync(ImportUserInput input)
        {
            return (await _userService.ImportAsync(input.Users)).ResultFormat("批量导入失败");
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<bool>> AddAsync(AddUserInput input)
        {
            return (await _userService.AddAsync(input)).ResultFormat("添加失败");
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<bool>> PasswordResetAsync(PasswordResetInput input)
        {
            return (await _userService.PasswordResetAsync(input)).ResultFormat("重置失败");
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<bool>> UpdatePasswordAsync(UpdatePasswordInput input)
        {
            return (await _userService.UpdatePasswordAsync(input)).ResultFormat();
        }

        /// <summary>
        /// 修改手机号码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<bool>> UpdatePhoneNumberAsync(UpdatePhoneNumberInput input)
        {
            return (await _userService.UpdatePhoneNumberAsync(input)).ResultFormat();
        }

        /// <summary>
        /// 修改邮箱
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<bool>> UpdateEmailAsync(UpdateEmailInput input)
        {
            return (await _userService.UpdateEmailAsync(input)).ResultFormat();
        }

        /// <summary>
        /// 修改头像
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<bool>> UpdateAvatarAsync(UpdateAvatarInput input)
        {
            return (await _userService.UpdateAvatarAsync(input)).ResultFormat();
        }

        /// <summary>
        /// 更新用户基本信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<bool>> UpdateUserInfoAsync(UpdateUserInfoInput input)
        {
            return (await _userService.UpdateUserInfoAsync(input)).ResultFormat();
        }

        /// <summary>
        /// 分配权限
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<bool>> AssignPermissionsAsync(AssignPermissionsInput input)
        {
            return (await _userService.AssignPermissionsAsync(input)).ResultFormat("分配权限失败");
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ResultModel<bool>> UpdateAsync(UpdateUserInput input)
        {
            return (await _userService.UpdateAsync(input)).ResultFormat("更新失败");
        }

        /// <summary>
        /// 启用
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ResultModel<bool>> EnabledAsync([Required] long id)
        {
            return (await _userService.EnabledAsync(id)).ResultFormat("切换状态失败");
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ResultModel<bool>> DeleteAsync([Required] long id)
        {
            return (await _userService.DeleteAsync(id)).ResultFormat("删除失败");
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ResultModel<bool>> BatchDeleteAsync(BaseIdsInput input)
        {
            return (await _userService.BatchDeleteAsync(input.Ids)).ResultFormat("批量删除失败");
        }
    }
}